// packageH/project_verification/AppointmentRecord/AppointmentRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project_name: "",
    store_name: "",
    options: ["已预约", "已完成", "已取消"],
    showPicker: false,
    statusVal: 0,
    statusTxt: "已预约",
    info: [],

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0 //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.setData({
      statusVal: 0,
      statusTxt: '已预约'
    })
    this.getData();
  },
  init() {
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getMoreData() {
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total.page) {
      return
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        status: this.data.statusVal == 2 ? "-1" : this.data.statusVal,
        store_name: this.data.store_name,
        project_name: this.data.project_name,
        page: this.data.page
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.store-projects.frontend.expect.expectLog"),
        data: json,
      })
      this.data.isLoadMore = true;
      if (!data.result) {
        app.tips(data.msg)
        this.data.isLoadMore = false;
        this.setData({
          page: this.data.page - 1
        })
        return
      }
      this.setData({
        info: this.data.info.concat(data.data.data),
      })
    }
  },
  async getData() {
    let json = {
      status: this.data.statusVal == 2 ? "-1" : this.data.statusVal,
      store_name: this.data.store_name,
      project_name: this.data.project_name
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-projects.frontend.expect.expectLog"),
      data: json,
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    this.setData({
      info: data.data.data,
      isLoadMore: true,
      total_page: data.data.last_page
    })
    if (!this.data.total_page) {
      this.data.total_page = 0;
    }
  },
  search() {
    this.init();
    this.getData();
  },
  cancelAppointment(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.showModal({
      title: '',
      content: '您确定要取消此预约吗',
      complete: (res) => {
        if (res.confirm) {
          this.cancelPost(id);
        }
      }
    })
  },
  showPicker_Open() {
    this.setData({
      showPicker: true
    })
  },
  onConfirm(e) {
    console.log(e)
    this.setData({
      statusVal: e.detail.index,
      statusTxt: e.detail.value,
      showPicker: false,
    })
    this.getData();
  },
  onCancel() {
    this.setData({
      showPicker: false
    })
  },
  async cancelPost(id) {
    let json = {
      id: id
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-projects.frontend.expect.cancelExpect"),
      data: json,
    })
    if (!data.result) {
      return app.tips(data.msg)
    }
    app.tips(data.msg)
    this.init();
    this.getData();
  }
})